# -*- python -*-
# ex: set syntax=python:

# This is a sample buildmaster config file. It must be installed as
# 'master.cfg' in your buildmaster's base directory (although the filename
# can be changed with the --basedir option to 'mktap buildbot master').

# It has one job: define a dictionary named BuildmasterConfig. This
# dictionary has a variety of keys to control different aspects of the
# buildmaster. They are documented in docs/config.xhtml .


# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}


# pre-preqs for environments
from buildbot.scheduler import Scheduler
from buildbot.scheduler import AnyBranchScheduler


# Lets define the type of environment, this will allow you to load a 
# different config file depending on whether you are developing, or 
# in production
# This can be either 'production' or 'development'
environment= 'production'

####### BUILDSLAVES

# the 'bots' list defines the set of allowable buildslaves. Each element is a
# tuple of bot-name and bot-password. These correspond to values given to the
# buildslave's mktap invocation.

# now defined in production/development.cfg

# 'slavePortnum' defines the TCP port to listen on. This must match the value
# configured into the buildslaves (with their --master option)

#changed from 9989
#c['slavePortnum'] = 6666
c['slavePortnum'] = 9999

####### CHANGESOURCES

# the 'sources' list tells the buildmaster how it should find out about
# source code changes. Any class which implements IChangeSource can be added
# to this list: there are several in buildbot/changes/*.py to choose from.

c['change_source'] = [] 

# or, use a PBChangeSource, and then have your repository's commit script run
# 'buildbot sendchange', or contrib/svn_buildbot.py, or
# contrib/arch_buildbot.py :
#
#from buildbot.changes.pb import PBChangeSource
#c['sources'].append(PBChangeSource())

# borked b/c xml option isn't in go-oo bonsai
#from buildbot.changes.bonsaipoller import BonsaiPoller
#bonsai = BonsaiPoller("http://www.go-oo.org/bonsai", "OpenOffice2", "", "all", "/var/cvsup")
#c['sources'].append(bonsai)

####### SCHEDULERS

## configure the Schedulers

#now defined in production/development.cfg

####### BUILDERS

# the 'builders' list defines the Builders. Each one is configured with a
# dictionary, using the following keys:
#  name (required): the name used to describe this bilder
#  slavename (required): which slave to use, must appear in c['bots']
#  builddir (required): which subdirectory to run the builder in
#  factory (required): a BuildFactory to define how the build is run
#  periodicBuildTime (optional): if set, force a build every N seconds

# buildbot/process/factory.py provides several BuildFactory classes you can
# start with, which implement build processes for common targets (GNU
# autoconf projects, CPAN perl modules, etc). The factory.BuildFactory is the
# base class, and is configured with a series of BuildSteps. When the build
# is run, the appropriate buildslave is told to execute each Step in turn.

# the first BuildStep is typically responsible for obtaining a copy of the
# sources. There are source-obtaining Steps in buildbot/process/step.py for
# CVS, SVN, and others.

builders = []

from buildbot.process import factory
from buildbot.steps.source import CVS
from buildbot.steps.shell import Compile
from buildbot.steps.shell import ShellCommand
from buildbot.steps.python_twisted import Trial

from buildbot.steps.shell import WithProperties

#c['customBuildProperties'] = [installSet, languages]

#hacky hack hack
from buildbot.process.buildstep import LoggedRemoteCommand
from OOCompile import OOCompile

from buildbot.steps import source
Source = source.Source
class cws(Source):
  name = 'cws'

  def __init__(self, branch=None, **kwargs):
    self.branch = branch
    Source.__init__(self, **kwargs)
    #self.args['branch'] = branch

  def startVC(self, branch, revision, patch):
    self.args['branch'] = branch
    self.args['revision'] = revision
    self.args['patch'] = patch
    cmd = LoggedRemoteCommand("cws", self.args)
    self.startCommand(cmd)

source.CWS = cws

import os, os.path, re
from rfc822 import Message

from buildbot import util
#from buildbot.twcompat import implements 
from buildbot.changes import base, changes #, maildirtwisted 
from buildbot.changes import mail

#from OOAllCVS import OOMaildirSource
#if environment == 'production':
##  c['sources'] = [OOMaildirSource(maildir="/home/buildmaster/Maildir/")]

from buildbot.steps.shell import WithProperties
from OOUpload import MyUpload      

# the builder f1 should be depricated
f1 = factory.BuildFactory()

f1.addStep(source.CWS, branch="HEAD", mode="copy", workdir="workdir",
           timeout=7200)
f1.addStep(ShellCommand, description="Prep", haltOnFailure=True,
           workdir="workdir", command=["../../buildprep", WithProperties("%(branch)s")])
f1.addStep(ShellCommand, description="Configure", haltOnFailure=True,
           workdir="workdir", command=["../../configure", WithProperties("%(branch)s")])
f1.addStep(ShellCommand, description="Bootstrap", haltOnFailure=True,
           workdir="workdir", command=["../../bootstrap", WithProperties("%(branch)s")])
f1.addStep(OOCompile, description="Compile", haltOnFailure=True,
           workdir="workdir", command=["../../compile", WithProperties("%(branch)s")])
f1.addStep(ShellCommand, description="Smoketest", haltOnFailure=False,
           workdir="workdir", command=["../../smoketestoo", WithProperties("%(branch)s")])



# cloph's builder
from buildbot.steps.transfer import FileUpload
from OOShell import OOShellCommand
f2 = factory.BuildFactory()
f2.addStep(OOShellCommand, description="Everything", haltOnFailure=True,
                      workdir='/home/buildslave/botwork',
                      command=["tinget.pl",
                               WithProperties("--branch=%s", "branch"),
                               WithProperties("--revision=%s", "revision"),
                               WithProperties("--languages=\"%s\"", "languages"),
                               WithProperties("--install_set=%s", "install_set"),
                               WithProperties("--buildnumber=%s", "buildnumber"),
                               WithProperties("--buildername=%s", "buildername")])

# f2.addStep(source.CWS, branch="HEAD", mode="copy", workdir="workdir",
#            timeout=7200)
f2.addStep(OOShellCommand, description="Prep", haltOnFailure=True,
           workdir="workdir", command=["../../buildprep"])
f2.addStep(OOShellCommand, description="Configure", haltOnFailure=True,
           workdir="workdir", command=["../../configure"])
f2.addStep(OOShellCommand, description="Bootstrap", haltOnFailure=True,
           workdir="workdir", command=["../../bootstrap"])
f2.addStep(OOCompile, description="Compile", haltOnFailure=True,
           workdir="workdir", command=["../../compile"])
f2.addStep(OOShellCommand, description="Smoketest", haltOnFailure=False,
           workdir="workdir", command=["../../smoketestoo"])
f2.addStep(OOShellCommand, description="Bundle",
           haltOnFailure=True,
           workdir="workdir",
           command=["../../bundle",
                   "install_set.zip"])
f2.addStep(MyUpload, workdir="workdir",
           blocksize=640*1024,
           slavesrc="install_set.zip",
           mode=0644,
           masterdest="install_set.zip")

f3 = factory.BuildFactory()

# new cws step development builder
f6.addStep(OOShellCommand, description="CWS", name="CWS",  haltOnFailure=True,
                      workdir='.',
                      command=["../cws_get", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"), 
                               WithProperties("--install_set=%s", "install_set")
				],
                      timeout=14400,
           want_stderr=True)
f6.addStep(OOShellCommand, description="Prep", name="Prep", haltOnFailure=True,
           workdir="workdir", command=["../../buildprep", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")])
f6.addStep(OOShellCommand, description="Configure", name="Configure", haltOnFailure=True,
           workdir="workdir", command=["../../configure", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")])
f6.addStep(OOShellCommand, description="Bootstrap", name="Bootstrap", haltOnFailure=True,
           workdir="workdir", command=["../../bootstrap", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")])
f6.addStep(OOCompile, description="Compile", name="Compile", haltOnFailure=True,
           workdir="workdir", command=["../../compile", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")],
           timeout=5000)
f6.addStep(OOShellCommand, description="Smoketest", name="Smoketest", haltOnFailure=False,
           workdir="workdir", command=["../../smoketestoo", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")],
           timeout=3000)
f6.addStep(OOShellCommand, description="Bundle Installset", name="Bundle Installset", haltOnFailure=False,
           workdir="workdir", command=["../../bundle", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")])
f6.addStep(MyUpload, workdir="workdir",
           blocksize=640*1024,
           slavesrc="install_set.zip",
           mode=0644,
           masterdest="install_set.zip")

f3 = factory.BuildFactory()

# With cws_get_svn step
f6.addStep(OOShellCommand, description="CWS", name="CWS",  haltOnFailure=True,
                      workdir='.',
                      command=["../cws_get_svn", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"), 
                               WithProperties("--install_set=%s", "install_set")
				],
                      timeout=14400,
           want_stderr=True)
f6.addStep(OOShellCommand, description="Prep", name="Prep", haltOnFailure=True,
           workdir="workdir", command=["../../buildprep", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")])
f6.addStep(OOShellCommand, description="Configure", name="Configure", haltOnFailure=True,
           workdir="workdir", command=["../../configure", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")])
f6.addStep(OOShellCommand, description="Bootstrap", name="Bootstrap", haltOnFailure=True,
           workdir="workdir", command=["../../bootstrap", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")])
f6.addStep(OOCompile, description="Compile", name="Compile", haltOnFailure=True,
           workdir="workdir", command=["../../compile", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")],
           timeout=5000)
f6.addStep(OOShellCommand, description="Smoketest", name="Smoketest", haltOnFailure=False,
           workdir="workdir", command=["../../smoketestoo", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")],
           timeout=3000)
f6.addStep(OOShellCommand, description="Bundle Installset", name="Bundle Installset", haltOnFailure=False,
           workdir="workdir", command=["../../bundle", 
                               WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")])
f6.addStep(MyUpload, workdir="workdir",
           blocksize=640*1024,
           slavesrc="install_set.zip",
           mode=0644,
           masterdest="install_set.zip")

# for "source" builder
f4 = factory.BuildFactory()
f4.addStep(OOShellCommand, description="CWS", haltOnFailure=True,
                      workdir='.',
                      command=["../cws_get", 
                                WithProperties("--branch=%s", "branch"),
                                WithProperties("--buildername=%s", "buildername"),
                                WithProperties("--buildnumber=%s", "buildnumber"),
                                WithProperties("--slavename=%s", "slavename"),
                                WithProperties("--revision=%s", "revision"),
                                WithProperties("--languages=\"%s\"", "languages"),
                                WithProperties("--install_set=%s", "install_set")],
                      timeout=7200,
           want_stderr=True)
f4.addStep(OOShellCommand, description="SLOC", haltOnFailure=True,
           workdir='workdir',
           command=["../../sloccount",
                    WithProperties("--branch=%s", "branch"),
                    WithProperties("--buildername=%s", "buildername"),
                    WithProperties("--buildnumber=%s", "buildnumber"),
                    WithProperties("--slavename=%s", "slavename"),
                    WithProperties("--revision=%s", "revision"),
                    WithProperties("--languages=\"%s\"", "languages"),
                    WithProperties("--install_set=%s", "install_set")],
                    timeout=2000)


# builders defined in productio/development.cfg

###### ENVIRONEMNT
f= open('%s.cfg' % environment)
exec f
f.close()



####### STATUS TARGETS

# 'status' is a list of Status Targets. The results of each build will be
# pushed to these targets. buildbot/status/*.py has a variety to choose from,
# including web pages, email senders, and IRC bots.

from buildbot.status import words
from OOtinder import OOTinderboxMailNotifier
from buildbot.status import mail
from buildbot.status import html

c['status'] = []

from buildbot.status.html import WebStatus
#c['status'].append(html.Waterfall(http_port=8090))
c['status'].append(WebStatus(http_port=8080,allowForce=True))

if environment == 'production':
  c['status'].append(words.IRC(host="irc.freenode.net", nick="oobuildbot",
                               channels=["#unhack-oo","#dev.OpenOffice.org","#ooo_macport","#go-oo"]))
  
  c['status'].append(OOTinderboxMailNotifier(fromaddr="buildermaster@termite.go-oo.org",
                                             logCompression="gzip",
                                             extraRecipients=[("ooo@go-oo.org"),("smsm1@openoffice.org"),("mikeleib@openoffice.org")],
                                             builders=["etch-gij","Mac-x86", "edgy-jdk", "Mac-PPC", "Solaris-Intel", "O3-build", "Solaris-Sparc", "Ubuntu", "Win-XP2", "MacIntel"]))
  
  c['status'].append(mail.MailNotifier(fromaddr="mikeleib@termite.go-oo.org",
                                       lookup='openoffice.org',
                                       extraRecipients=[("buildbot@shaunmcdonald.me.uk")],
                                       sendToInterestedUsers=False))

#
# from buildbot.status import client
# c['status'].append(client.PBListener(9988))


####### DEBUGGING OPTIONS

# if you set 'debugPassword', then you can connect to the buildmaster with
# the diagnostic tool in contrib/debugclient.py . From this tool, you can
# manually force builds and inject changes, which may be useful for testing
# your buildmaster without actually commiting changes to your repository (or
# before you have a functioning 'sources' set up). The debug tool uses the
# same port number as the slaves do: 'slavePortnum'.

#c['debugPassword'] = "debugpassword"

# if you set 'manhole', you can ssh into the buildmaster and get an
# interactive python shell, which may be useful for debugging buildbot
# internals. It is probably only useful for buildbot developers. You can also
# use an authorized_keys file, or plain telnet.
#from buildbot import manhole
#c['manhole'] = manhole.PasswordManhole("tcp:9999:interface=127.0.0.1",
#                                       "admin", "password")


####### PROJECT IDENTITY

# the 'projectName' string will be used to describe the project that this
# buildbot is working on. For example, it is used as the title of the
# waterfall HTML page. The 'projectURL' string will be used to provide a link
# from buildbot HTML pages to your project's home page.

c['projectName'] = "OpenOffice.org"
c['projectURL'] = "http://openoffice.org/"

# the 'buildbotURL' string should point to the location where the buildbot's
# internal web server (usually the html.Waterfall page) is visible. This
# typically uses the port number set in the Waterfall 'status' entry, but
# with an externally-visible host name which the buildbot cannot figure out
# without some help.

c['buildbotURL'] = "http://termite.go-oo.org/buildbot/"
